package com.erp.controller;


import com.erp.entity.RolePerms;
import com.erp.entity.result.Result;
import com.erp.entity.result.ResultStatus;
import com.erp.service.RolePermsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author pyj
 * @since 2020-11-07
 */
@RestController
@RequestMapping("/role-perms")
public class RolePermsController {

    @Autowired
    private RolePermsService rolePermsService;

    @GetMapping("/findRolePermsByName")
    public Result findRolePermsByName(String name) {
        return new Result(ResultStatus.SUCCESS, rolePermsService.findRolePermsByName(name));
    }

    @GetMapping("/getRolePermsByPage")
    public Result getRolePermsByPage(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                                @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize) {

        PageHelper.startPage(page,pageSize);
        List<RolePerms> list = rolePermsService.getAll();
        PageInfo<RolePerms> pageInfo = new PageInfo<>(list);
        return new Result(ResultStatus.SUCCESS, pageInfo);
    }


    @PostMapping("/add")
    public Result insert(@RequestBody RolePerms rolePerms) {

        if (rolePermsService.add(rolePerms) > 0) {
            return new Result(ResultStatus.SUCCESS, null);
        } else {
            return new Result(ResultStatus.BAD_REQUEST, null);
        }
    }

    @GetMapping("/delete")
    public Result delete(@PathParam("id") Integer id) {
        if (rolePermsService.delete(id) > 0) {
            return new Result(ResultStatus.SUCCESS, null);
        } else {
            return new Result(ResultStatus.BAD_REQUEST, null);
        }
    }

    @PostMapping("/update")
    public Result update(@RequestBody RolePerms rolePerms) {

        if (rolePermsService.update(rolePerms) > 0) {
            return new Result(ResultStatus.SUCCESS, null);
        } else {
            return new Result(ResultStatus.BAD_REQUEST, null);
        }
    }
}

